﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;

namespace HFController
{
    /// <summary>
    /// 新闻业务逻辑
    /// </summary>
    public class NewsController:ControllerBase
    {
        //本类单列对象
        private static NewsController _newsController;

        //实例化单列对象
        public static NewsController getController()
        {
            if (_newsController == null)
            {
                _newsController = new NewsController();
            }
            return _newsController;
        }
        #region 新闻专题

        /// <summary>
        /// 新闻专题数据库访问
        /// </summary>
        private hf.DAL.TopicClass TopicClassDAL = new hf.DAL.TopicClass();

        /// <summary>
        /// 添加新闻专题
        /// </summary>
        /// <param name="TopicName"></param>
        public void doAddTopicClass(string TopicName)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            hf.Model.TopicClass model = new hf.Model.TopicClass();

            model.TopicName = TopicName;

            try
            {
                model.ID = TopicClassDAL.Add(model);

                result.isTrue = true;
                result.Message = "添加成功！";
                result.goUrl = "location='TopicClass.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }
        /// <summary>
        /// 根据ID 得到一条新闻专题
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public hf.Model.TopicClass getTopicClassModel(int ID)
        {
            hf.Model.TopicClass model = new hf.Model.TopicClass();

            int total;
            List<hf.Model.TopicClass> list = TopicClassDAL.GetTopicClassList(1, 1, "ID=" + ID.ToString(), out total);
            if (list.Count > 0)
            {
                model = list[0];
            }
            else
            {
                model = null;
            }
            return model;
        }

        /// <summary>
        /// 修改新闻专题
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="TopicName"></param>
        public void doModifyTopicClass(int ID, string TopicName)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();

            hf.Model.TopicClass model = this.getTopicClassModel(ID);

            model.TopicName = TopicName;
            try
            {
                //执行修改
                TopicClassDAL.Update(model);

                result.isTrue = true;
                result.Message = "修改成功！";
                result.goUrl = "location='TopicClass.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 得到专题列表用于绑定
        /// </summary>
        /// <param name="LoadCount"></param>
        /// <returns></returns>
        public List<hf.Model.TopicClass> getTopicList(int LoadCount)
        {
            List<hf.Model.TopicClass> list = new List<hf.Model.TopicClass>();
            //总数据记录
            int total;

            //筛选条件
            string strSql = "";


            //获得列表
            list = TopicClassDAL.GetTopicClassList(LoadCount, 1, strSql, out total);

            return list;
        }

        /// <summary>
        /// 得到新闻专题列表
        /// </summary>
        /// <param name="PagerStr"></param>
        /// <returns></returns>
        public List<hf.Model.TopicClass> getTopicClassList(out string PagerStr)
        {
            List<hf.Model.TopicClass> list = new List<hf.Model.TopicClass>();

            //每页显示的记录
            int PageSize = 10;
            //当前页码
            int PageNumber = 1;
            if (HttpContext.Current.Request.QueryString["Page"] != null)
            {
                PageNumber = int.Parse(HttpContext.Current.Request.QueryString["Page"].ToString());
            }

            //总数据记录
            int total;

            //筛选条件
            string strSql = "";

            //获得列表
            list = TopicClassDAL.GetTopicClassList(PageSize, PageNumber, strSql, out total);

            //分页
            PagerStr = LLL.Pager.TrunPageHtmlCode(PageSize, PageNumber, total, "");

            return list;
        }

        /// <summary>
        /// 删除新闻专题
        /// </summary>
        /// <param name="ID"></param>
        public void doDelegeTopicClass(int ID)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();

            try
            {
                TopicClassDAL.Delete(ID);

                result.isTrue = true;
                result.Message = "删除成功！";
                result.goUrl = "location='TopicClass.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        } 
        #endregion

        #region 新闻

        /// <summary>
        /// 新闻数据库访问
        /// </summary>
        hf.DAL.News NewsDAL = new hf.DAL.News();
        
        /// <summary>
        /// 添加新闻
        /// </summary>
        /// <param name="ClassID"></param>
        /// <param name="bClassID"></param>
        /// <param name="sClassID"></param>
        /// <param name="TopicID"></param>
        /// <param name="Title"></param>
        /// <param name="PicTitle"></param>
        /// <param name="Overview"></param>
        /// <param name="Content"></param>
        /// <param name="ByUser"></param>
        /// <param name="ByFrom"></param>
        /// <param name="isIndex"></param>
        /// <param name="isRotation"></param>
        public void doAddNews(string ClassID, string bClassID, string sClassID, string TopicID, string Title, FileUpload PicTitle, string Overview, string Content, string ByUser, string ByFrom, int isIndex, int isRotation,int isTop ,string isGo, int CityID)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            hf.Model.News model = new hf.Model.News();

            //实例化上传类
            PictureCommand cmd = new PictureCommand();

            //图片标题地址
            string PicUrl = "";
            //判断是否包含上传文件
            if (PicTitle.HasFile)
            {
                //是否生成缩略图
                cmd.IsCreate = false;
                //这个PicTitle是上传控
                if (cmd.UpLoadIMG(PicTitle))
                {
                    //上传成功
                    //得到上传的文件名
                    PicUrl = cmd.OFileName;
                }
            }
            model.ClassID = int.Parse(ClassID);
            model.bClassID = int.Parse(bClassID);
            model.sClassID = int.Parse(sClassID);
            model.TopicID = int.Parse(TopicID);
            model.Title = Title;
            model.PicTitle = PicUrl;
            model.Overview = Overview;
            model.Content = Content;
            model.Addtime = DateTime.Now;
            model.ByUser = ByUser;
            model.ByFrom = ByFrom;
            model.isIndex = isIndex;
            model.Hits = 0;
            model.isTop = isTop;
            model.isRotation = isRotation;
            model.CityID = CityID;
            try
            {
                model.ID = NewsDAL.Add(model);

                result.isTrue = true;
                result.Message = "添加成功！";
                if (isGo == "1")
                {
                    result.goUrl = "location='News.aspx';";
                }
                else
                {
                    result.goUrl = "location='NewsManage.aspx';";
                }
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 根据一个ID 得到一条新闻
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public hf.Model.News getNewsModel(int ID)
        {
            hf.Model.News model = new hf.Model.News();
            int total;

            List<hf.Model.News> list = NewsDAL.GetNewsList(1, 1, "ID=" + ID.ToString(), out total);
            if (list.Count > 0)
            {
                model = list[0];
                model.Hits++;
                NewsDAL.Update(model);
            }
            else
            {
                model = null;
            }
            return model;
        }

        /// <summary>
        /// 修改新闻
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="ClassID"></param>
        /// <param name="bClassID"></param>
        /// <param name="sClassID"></param>
        /// <param name="TopicID"></param>
        /// <param name="Title"></param>
        /// <param name="PicTitle"></param>
        /// <param name="Overview"></param>
        /// <param name="Content"></param>
        /// <param name="ByUser"></param>
        /// <param name="ByFrom"></param>
        /// <param name="isIndex"></param>
        /// <param name="Hits"></param>
        /// <param name="isRotation"></param>
        public void doModifyNews(int ID, string ClassID, string bClassID, string sClassID, string TopicID, string Title, FileUpload PicTitle, string Overview, string Content, string ByUser, string ByFrom, int isIndex, string Hits, int isRotation ,int isTop, int CityID)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            hf.Model.News model = this.getNewsModel(ID);

            //大图url 
            string PicUrl1 = "";

            //判断是否选择了文件
            if (PicTitle.HasFile)
            {
                //上传
                PictureCommand picCmd = new PictureCommand();
                picCmd.IsRate = false;
                picCmd.IsCreate = false;
                if (picCmd.UpLoadIMG(PicTitle))
                {
                    //大图的URL
                    PicUrl1 = picCmd.OFileName;
                }
            }

            model.ClassID = int.Parse(ClassID);
            model.bClassID = int.Parse(bClassID);
            model.sClassID = int.Parse(sClassID);
            model.TopicID = int.Parse(TopicID);
            model.Title = Title;
            model.PicTitle = PicUrl1;
            model.Overview = Overview;
            model.Content = Content;
            model.ByUser = ByUser;
            model.ByFrom = ByFrom;
            model.isIndex = isIndex;
            model.Hits = int.Parse(Hits);
            model.isRotation = isRotation;
            model.isTop = isTop;
            model.CityID = CityID;

            try
            {
                //执行修改
               NewsDAL.Update(model);

                result.isTrue = true;
                result.Message = "修改成功！";
                result.goUrl = "location='NewsManage.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 得到新闻列表
        /// </summary>
        /// <param name="PagerStr"></param>
        /// <returns></returns>
        public List<hf.Model.News> getNewsList(out string PagerStr)
        {
            List<hf.Model.News> list = new List<hf.Model.News>();

            //没有显示记录
            int PageSize = 10;
            //当前页码
            int PageNumber = 1;
            if (HttpContext.Current.Request.QueryString["Page"] != null)
            {
                PageNumber = int.Parse(HttpContext.Current.Request.QueryString["Page"].ToString());
            }
            //记录总条数
            int total;
            //筛选条件
            string strSql = "";

            //获得列表
            list = NewsDAL.GetNewsList(PageSize, PageNumber, strSql, out total);

            //分页
            PagerStr = LLL.Pager.TrunPageHtmlCode(PageSize, PageNumber, total, "");

            return list;
        }

        /// <summary>
        /// 得到前台新闻列表
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageNumber"></param>
        /// <param name="strWhere"></param>
        /// <param name="Total"></param>
        /// <returns></returns>
        public List<hf.Model.News> getNewsList(int PageSize, int PageNumber, string strWhere, out int Total)
        {
            List<hf.Model.News> list = new List<hf.Model.News>();

            //获得列表
            list = NewsDAL.GetNewsList(PageSize, PageNumber, strWhere, out Total);

            return list;
        }

        /// <summary>
        /// 根据搜索条件得到列表
        /// </summary>
        /// <param name="strWhere"></param>
        /// <param name="PageStr"></param>
        /// <returns></returns>
        public List<hf.Model.News> getNewsList(string strWhere, out string PagerStr)
        {
            List<hf.Model.News> list = new List<hf.Model.News>();

            //没有显示记录
            int PageSize = 10;
            //当前页码
            int PageNumber = 1;
            if (HttpContext.Current.Request.QueryString["Page"] != null)
            {
                PageNumber = int.Parse(HttpContext.Current.Request.QueryString["Page"].ToString());
            }
            //记录总条数
            int total;

            //获得列表
            list = NewsDAL.GetNewsList(PageSize, PageNumber, strWhere, out total);

            //分页
            PagerStr = LLL.Pager.TrunPageHtmlCode(PageSize, PageNumber, total, "");

            return list;
        }

        /// <summary>
        /// 得到首页推荐和置顶
        /// </summary>
        /// <param name="Count"></param>
        /// <param name="isIndex"></param>
        /// <param name="isTop"></param>
        /// <returns></returns>
        public List<hf.Model.News> getNewsList(int Count, int isIndex, int isTop)
        {
            List<hf.Model.News> list = new List<hf.Model.News>();
            //没有显示记录
            int PageSize = Count;
            //当前页码
            int PageNumber = 1;
            //记录总条数
            int total;
            //获得列表
            list = NewsDAL.GetNewsList(PageSize, PageNumber, "isTop=" + isTop.ToString() + " and isIndex=" + isIndex.ToString(), out total);
            return list;
        }
        /// <summary>
        /// 根据搜索条件得到指定数量的新闻
        /// </summary>
        /// <param name="Count"></param>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public List<hf.Model.News> getNewsList(int Count, string strWhere)
        {
            List<hf.Model.News> list = new List<hf.Model.News>();
            //当前页码
            int PageNumber = 1;
            //记录总条数
            int total;
            //获得列表
            list = NewsDAL.GetNewsList(Count, PageNumber, strWhere, out total);
            this.bindParameter(list);
            return list;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Count"></param>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public List<hf.Model.News> getNewsListForAjax(int Count, string strWhere)
        {
            List<hf.Model.News> list = new List<hf.Model.News>();
            //当前页码
            int PageNumber = 1;
            //记录总条数
            int total;
            //获得列表
            list = NewsDAL.GetNewsList(Count, PageNumber, strWhere, out total);
            BagePages bp = new BagePages();
            foreach (hf.Model.News n in list)
            {
                n.Title = bp.CutString(n.Title, 30, true);
            }

            return list;
        }

        /// <summary>
        /// 得到有图片轮播的新闻
        /// </summary>
        /// <param name="Count"></param>
        /// <param name="isRotation"></param>
        /// <returns></returns>
        public List<hf.Model.News> getNewsList(int Count, int isRotation)
        {
            List<hf.Model.News> list = new List<hf.Model.News>();
            //没有显示记录
            int PageSize = 10;
            //当前页码
            int PageNumber = 1;
            //记录总条数
            int total;
            //获得列表
            list = NewsDAL.GetNewsList(PageSize, PageNumber, "isRotation=" + isRotation.ToString() , out total);
            return list;
        }

        /// <summary>
        /// 绑定新闻参数
        /// </summary>
        /// <param name="list"></param>
        private void bindParameter(List<hf.Model.News> list)
        {
            /*
            BagePages bp = new BagePages();
            foreach (hf.Model.News n in list)
            {
                n.Title = bp.CutString(n.Title, 30, true);
            }
             * */
        }

        /// <summary>
        /// 删除新闻
        /// </summary>
        /// <param name="ID"></param>
        public void doDelegeNews(int ID)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();

            try
            {
               NewsDAL.Delete(ID);

                result.isTrue = true;
                result.Message = "删除成功！";
                result.goUrl = "location='NewsManage.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 批量删除新闻
        /// </summary>
        /// <param name="IDS"></param>
        public void doDelegeNews(string[] IDS)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            try
            {
                for (int i = 0; i < IDS.Length; i++)
                {
                    NewsDAL.Delete(int.Parse(IDS[i].Trim()));
                }
                result.isTrue = true;
                result.Message = "删除成功！";
                result.goUrl = "location='NewsManage.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }
        #endregion
    }
}
       